package com.facebook.android;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.facebook.android.Example.SampleDialogListener;
import com.facebook.android.Example.WallPostRequestListener;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;


public class Weather extends Activity implements OnClickListener {
	public static final String APP_ID = "148014628599651";
	private int choice = -1;
	private List<MyData> data = new ArrayList<MyData>();
	private String zip;
	
	private Facebook mFacebook;
	private AsyncFacebookRunner mAsyncRunner;

	private Button submit;
	
	 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.weather);
		data.add(new MyData());
		data.add(new MyData());
		data.add(new MyData());
		data.get(0).tv = ((TextView) findViewById(R.id.txt1));
		data.get(1).tv = ((TextView) findViewById(R.id.txt2));
		data.get(2).tv = ((TextView) findViewById(R.id.txt3));
		data.get(0).iv = ((ImageView) findViewById(R.id.icon1));
		data.get(1).iv = ((ImageView) findViewById(R.id.icon2));
		data.get(2).iv = ((ImageView) findViewById(R.id.icon3));
		data.get(0).rb = ((RadioButton) findViewById(R.id.day1));
		data.get(1).rb = ((RadioButton) findViewById(R.id.day2));
		data.get(2).rb = ((RadioButton) findViewById(R.id.day3));
		
		mFacebook = new Facebook(APP_ID);
		mAsyncRunner = new AsyncFacebookRunner(mFacebook);
		
		RadioSelect rbOnClick = new RadioSelect();
		for(MyData myd: data){
			myd.rb.setOnClickListener(rbOnClick);
		}
		submit = (Button) findViewById(R.id.postButton);
		submit.setOnClickListener(this);
		
		
		Bundle extras = getIntent().getExtras();
		if (extras == null) {
			return;
		}
		
		if(extras.getString("zip") != null)
			zip = extras.getString("zip");
			
		for(int i = 0; i < 3; i++){
			data.get(i).time = extras.getString("time" + i);
			data.get(i).cond = extras.getString("condition" + i);
			data.get(i).temp = extras.getString("temperature" + i);
			data.get(i).chart = extras.getString("chart" + i);
			if((data.get(i).time != null) && (data.get(i).cond != null) &&
			  (data.get(i).temp != null) && (extras.getString("feels_like" + i) != null) &&
			  (extras.getString("wind" + i) != null) && (data.get(i).chart != null)){
				data.get(i).tv.setText(data.get(i).time + " " + data.get(i).cond + " " + 
						data.get(i).temp + " " + extras.getString("feels_like" + i) + " " +
				  extras.getString("wind" + i));
				data.get(i).iv.setImageDrawable(getImage(data.get(i).chart));
			}
		}
		/*
		String value1 = extras.getString("Value1");
		String value2 = extras.getString("Value2");
		if (value1 != null && value2 != null) {
			
		}
		*/
	}
	
	private Drawable getImage(String address){
		try {
			URL url = new URL(address);
			InputStream is = (InputStream) url.getContent();
			Drawable d = Drawable.createFromStream(is, "src");
			return d;
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public void onClick(View v) {
		RadioButton rb;
		for(int i = 0; i < data.size(); i++){
			rb = data.get(i).rb;
			if(rb.isChecked()){
				choice = i;
				String message = "The weather prediction for " + data.get(i).time +
				  " is " + data.get(i).cond + " and the temperature will be " +
				  data.get(i).temp;
				Bundle parameters = new Bundle();
				parameters.putString("message", "Nice! I like that.");
				parameters.putString("picture", data.get(i).chart);
				parameters.putString("link", "http://www.weather.com/weather/today/" + zip);
				parameters.putString("name", message);
				parameters.putString("caption", " ");
				parameters.putString("description", " ");
                mFacebook.dialog(Weather.this, "feed", parameters,
                        new SampleDialogListener());
				
			}
		}
	}
	
	private class RadioSelect implements OnClickListener{
		public void onClick(View v) {
			submit.setVisibility(View.VISIBLE);
		}
	}
	
	public class WallPostRequestListener extends BaseRequestListener {

        public void onComplete(final String response, final Object state) {
            Log.d("Facebook-Example", "Got response: " + response);
            String message = "<empty>";
            try {
                JSONObject json = Util.parseJson(response);
                message = json.getString("message");
            } catch (JSONException e) {
                Log.w("Facebook-Example", "JSON Error in response");
            } catch (FacebookError e) {
                Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
            }
            /*
            final String text = "Post Successful!";
            Weather.this.runOnUiThread(new Runnable() {
                public void run() {
                    mText.setText(text);
                }
            })*/;
        }
    }
	
	public class SampleDialogListener extends BaseDialogListener {

        public void onComplete(Bundle values) {
            final String postId = values.getString("post_id");
            if (postId != null) {
                Log.d("Facebook-Example", "Dialog Success! post_id=" + postId);
                mAsyncRunner.request(postId, new WallPostRequestListener());
            } else {
                Log.d("Facebook-Example", "No wall post made");
            }
        }
    }
}

class MyData{
	String time, temp, cond, chart;
	TextView tv;
	ImageView iv;
	RadioButton rb;
}
